﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Globalization;

namespace ServerMonitor.Common
{
    public class Minfo
    {

         









        //zongdaxiao = GetHardDiskSpace("C") * 1.0 / 1024;
        //user = GetHardDiskFreeSpace("C") * 1.0 / 1024;





        ///// <summary>
        ///// 获取CPU使用情况
        ///// </summary>
        ///// <returns></returns>

        //PerformanceCounter pcCpuLoad = new PerformanceCounter("Processor", "% Processor Time", "_Total");
        //public int GetCpuRate()
        //{

        //    //PerformanceCounter PC = new PerformanceCounter("Processor", "% Processor Time", "_Total");
        //    //return Convert.ToInt32(PC.NextValue());



             
        //    return  Convert.ToInt32(pcCpuLoad.NextValue().ToString(CultureInfo.InvariantCulture));


        //}
         
      


        /// <summary>
        /// 获取物理内存使用情况
        /// </summary>
        /// <returns></returns>
        public int GetMemoryRate()
        {
            GlobalMemoryStatus(ref MemInfo);
            long totalMb = Convert.ToInt64(MemInfo.dwTotalPhys.ToString()) / 1024 / 1024;
            long avaliableMb = Convert.ToInt64(MemInfo.dwAvailPhys.ToString()) / 1024 / 1024;
            return Convert.ToInt32(avaliableMb*100 / totalMb);
        }

        /// <summary>
        /// 获取硬盘使用率
        /// </summary>
        /// <param name="DiskName"></param>
        /// <returns></returns>
        public int GetHardRate(string DiskName)
        {
            return 100- Convert.ToInt32(GetHardDiskFreeSpace(DiskName)*100 / GetHardDiskSpace(DiskName));
        }


        /// <summary>
        /// 获取指定驱动器的空间总大小(单位为B) 
        /// </summary>
        /// <param name="str_HardDiskName">只需输入代表驱动器的字母即可 （大写）</param>
        /// <returns></returns>
        public long GetHardDiskSpace(string str_HardDiskName)
        {
            long totalSize = new long();
            str_HardDiskName = str_HardDiskName + ":\\";
            System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();
            foreach (System.IO.DriveInfo drive in drives)
            {
                if (drive.Name == str_HardDiskName)
                {
                    totalSize = drive.TotalSize / (1024 * 1024);
                }
            }
            return totalSize;
        }
         
        /// <summary>
        /// 获取指定驱动器的剩余空间总大小(单位为B) 
        /// </summary>
        /// <param name="str_HardDiskName">只需输入代表驱动器的字母即可  </param>
        /// <returns></returns> 
        public long GetHardDiskFreeSpace(string str_HardDiskName)
        {
            long freeSpace = new long();
            str_HardDiskName = str_HardDiskName + ":\\";
            System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();
            foreach (System.IO.DriveInfo drive in drives)
            {
                if (drive.Name == str_HardDiskName)
                {
                    freeSpace = drive.TotalFreeSpace / (1024 * 1024);
                }
            }
            return freeSpace;
        }

        //获取总内存（运行）
        //zongneicun = GetMemoryStatus();

        [StructLayout(LayoutKind.Sequential)]
        public struct MEMORY_INFO
        {
            public uint dwLength;
            public uint dwMemoryLoad;
            public uint dwTotalPhys;
            public uint dwAvailPhys;
            public uint dwTotalPageFile;
            public uint dwAvailPageFile;
            public uint dwTotalVirtual;
            public uint dwAvailVirtual;
        }
        [DllImport("kernel32")]
        public static extern void GlobalMemoryStatus(ref MEMORY_INFO meminfo);
        MEMORY_INFO MemInfo = new MEMORY_INFO();

        public long GetMemoryStatus()
        {
            GlobalMemoryStatus(ref MemInfo);
            long totalMb = Convert.ToInt64(MemInfo.dwTotalPhys.ToString()) / 1024 / 1024;
            long avaliableMb = Convert.ToInt64(MemInfo.dwAvailPhys.ToString()) / 1024 / 1024;
            return totalMb;
            //MessageBox.Show("物理内存共有" + totalMb.ToString () + " MB" + "可使用的物理内存有" + avaliableMb.ToString () + " MB");
            //Console.WriteLine("物理内存共有" + totalMb + " MB");
            //Console.WriteLine("可使用的物理内存有" + avaliableMb + " MB");
        }



        //获得pro使用内存
        public int pnc()
        {
            Process[] pro = Process.GetProcesses();
            double total = 0;
            Process temp;
            int i;
            for (i = 0; i < pro.Length; i++)
            {
                temp = pro[i];
                total = temp.PrivateMemorySize + total;
            }
            return (int)(total / 1024 / 1024);
        }

    }
}